#include "qmyPLTM.h"
#include "qizvodvnes.h"
#include "qizvodkorekcija.h"


QMyPLTM::QMyPLTM(QWidget *parent)
: QMyBaseWidget(parent)
,PLTMVnes(0)
,PLTMKorekcija(0)
,PLTMLista(0)
,m_row(0)
{
	ui.setupUi(this);
	pressF4();
}

QMyPLTM::~QMyPLTM()
{

}

void QMyPLTM::refresh()
{
}
void QMyPLTM::pressF2()
{
	closeAllForm();
	PLTMVnes = showMyWidget<QPLTMVnes>(PLTMVnes); 
    PLTMVnes->setCategoryWidget(this);
	connect(PLTMVnes,SIGNAL(closeW()),this,SLOT(closeW()));
	connect(PLTMVnes,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
	connect(PLTMVnes,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	connect(PLTMVnes,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(PLTMVnes,SIGNAL(scallProPLTM(QWidget*, QString)),this,SLOT(callProPLTM(QWidget*, QString)));

	emit disablePrint(true);
}
void QMyPLTM::pressF3()
{
    if(!PLTMLista)
    {
        return;
    }
    int id = PLTMLista->getSelectedId();
    closeAllForm();
    PLTMKorekcija = showMyWidget<QPLTMKorekcija>(PLTMKorekcija); 
    PLTMKorekcija->setCategoryWidget(this);
    connect(PLTMKorekcija,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(PLTMKorekcija,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(PLTMKorekcija,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	connect(PLTMKorekcija,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(PLTMKorekcija,SIGNAL(scallProPLTM(QWidget*, QString)),this,SLOT(callProPLTM(QWidget*, QString)));
    PLTMKorekcija->init(id);
	emit disablePrint(true);
}
void QMyPLTM::pressF4()
{
    closeAllForm();
    PLTMLista = showMyWidget<QPLTMLista>(PLTMLista); 
    PLTMLista->setCategoryWidget(this);
	PLTMLista->setRow(m_row);
    connect(PLTMLista, SIGNAL(closeW()), this, SLOT(closeWLista()));
    connect(PLTMLista, SIGNAL(procF2()), this, SLOT(procF2()));
    connect(PLTMLista, SIGNAL(procF3()), this, SLOT(procF3()));
    connect(PLTMLista, SIGNAL(procDel()), this, SLOT(procDel()));
	connect(PLTMLista, SIGNAL(sPLTMToIzvod(QString&, QString&, QString&, QString&)), this, SLOT(retPLTMToIzvod(QString&, QString&, QString&, QString&)));
	emit disablePrint(false);

}
void QMyPLTM::procF2()
{
    pressF2();
}
void QMyPLTM::procF3()
{
    pressF3();
}
void QMyPLTM::procDel()
{

}
void QMyPLTM::pressEscape()
{
    this->raise();
    this->setFocus();
	emit disablePrint(true);

    emit signCloseMyWidget();
}
void QMyPLTM::closeW()
{
    pressF4();
}
void QMyPLTM::closeWLista()
{
    pressEscape();
}

void QMyPLTM::closeAllForm()
{
    PLTMKorekcija = deleteMyWidget<QPLTMKorekcija>(PLTMKorekcija);
    PLTMVnes = deleteMyWidget<QPLTMVnes>(PLTMVnes);
	if (PLTMLista)
	{
		m_row = PLTMLista->getRow();
	}
    PLTMLista = deleteMyWidget<QPLTMLista>(PLTMLista);
}
void QMyPLTM::callKomintenti(QWidget* myobj, QString text)
{
    emit signKomintentiFromIspratnica(myobj, text);
}

void QMyPLTM::callArtikli(QWidget* myobj, QString text)
{
    emit signArtikliFromIspratnica(myobj, text);
}

void QMyPLTM::callIspratnici(QWidget* myobj, QString text)
{
	emit signImportArtikliFromIspratnici(myobj, text);
}

void QMyPLTM::callProPLTM(QWidget* myobj, QString text)
{
	emit signImportArtikliFromProPLTM(myobj, text);
}

void QMyPLTM::sendPrintMessage()
{
    emit signPrint();
}

QStandardItemModel* QMyPLTM::getModel()
{
    QStandardItemModel * modelExt;
    if (PLTMLista)
    {
        modelExt = PLTMLista->model;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStandardItemModel* QMyPLTM::getModel2()
{
    QStandardItemModel * modelExt;
    if (PLTMLista)
    {
        modelExt = PLTMLista->model2;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStringList QMyPLTM::getIspratnicaInfo()
{
    QStringList modelExt;
    modelExt.clear();

    if (PLTMLista)
    {
        modelExt = PLTMLista->geInfo();
    } 
    else
    {
        modelExt;
    }
    return modelExt;
}

void QMyPLTM::retPLTMToIzvod(QString& text6, QString& text8, QString& text9, QString& textName)
{
	QWidget *sour = getSourceWidget();
	if (qobject_cast<QIzvodVnes*>(sour))
	{
		QIzvodVnes *dest = qobject_cast<QIzvodVnes*>(sour);
//		dest->setPLTM(text6, text8, text9, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QIzvodKorekcija*>(sour))
	{
		QIzvodKorekcija *dest = qobject_cast<QIzvodKorekcija*>(sour);
//		dest->setPLTM(text6, text8, text9, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
}

void QMyPLTM::setInitSearchText(QString text)
{
    if (PLTMLista)
    {
        PLTMLista->setInitText(text);
    } 
}